<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/12/3
 * Time: 15:54
 */
class Lottery_winner_model extends CI_Model
{
    private $_table  = 'lottery_winner';
    private $_table1 = 'lottery_prize';
    private $_table2 = 'express_record';

    /**
     * 插入中奖用户
     */
    public function addUser($postData)
    {
        $data = array(
            'lottery_id'   => $postData->lottery_id,
            'uid'          => $postData->uid,
            'user_name'    => $postData->user_name,
            'reseller_name'=> empty($postData->reseller_name) ? '' : $postData->reseller_name,
            'prize_id'     => $postData->prize_id,
            'prize_name'   => $postData->prize_name,
            'prize_type'   => $postData->type,
            'amount'       => $postData->amount,
            'grade'        => $postData->grade,
            'links'        => $postData->links,
            'status'       => isset($postData->status) ? $postData->status : 1,
            'address'      => '',
            'effective_date' => date('Y-m-d H:i:s', strtotime("+{$postData->effective_days} day")),
            'created_at'   => date('Y-m-d H:i:s',time()),
        );
        $this->db->insert($this->_table, $data);
        return $this->db->insert_id();
    }

    public function total($params)
    {
        $this->db->select('*');
        $this->db->from($this->_table);
        $this->db->join($this->_table1, 'lottery_winner.prize_id=lottery_prize.prize_id', 'INNER');
        $this->db->where('uid', $params['uid']);
        if ( !empty($params['lottery_id'])) {
            $this->db->where('lottery_winner.lottery_id', $params['lottery_id']);
        }
        return $this->db->count_all_results();
    }

    /**
     * 查找中奖记录
     * @param $uid
     * @return mixed
     */
    public function findByUId($params)
    {
        $this->db->select('lottery_winner.id, lottery_winner.uid, user_name, lottery_winner.prize_name, prize_type, lottery_winner.amount, lottery_winner.links, lottery_winner.status, lottery_winner.effective_date, address, lottery_winner.created_at, lottery_prize.image');
        $this->db->from($this->_table);
        $this->db->join($this->_table1, 'lottery_winner.prize_id=lottery_prize.prize_id', 'INNER');
        $this->db->where('lottery_winner.uid', $params['uid']);

        if ( !empty($params['lottery_id'])) {
            $this->db->where('lottery_winner.lottery_id', $params['lottery_id']);
        }

        $this->db->where('lottery_winner.created_at >', date('Y-m-d', strtotime('-1 month')));
        $this->db->order_by('id', 'DESC');
        return $this->db->get();
    }

    /**
     * 查找中奖记录
     * @param $uid
     * @return mixed
     */
    public function findById($id)
    {
        $this->db->select('lottery_winner.id, lottery_winner.uid, user_name, lottery_winner.prize_name, prize_type, lottery_winner.amount, lottery_winner.links, lottery_winner.status, lottery_winner.effective_date, address, lottery_winner.created_at, express_record.name, express_record.number,express_record.context, lottery_prize.image');
        $this->db->from($this->_table.' AS lottery_winner');
        $this->db->join($this->_table1, 'lottery_winner.prize_id=lottery_prize.prize_id', 'INNER');
        $this->db->join($this->_table2.' AS express_record', 'express_record.number=lottery_winner.express_num', 'LEFT');
        $this->db->where('lottery_winner.id', $id);
        $this->db->limit(1);
        return $this->db->get();
    }

    /**
     * 更新状态 地址
     * @param $id
     * @param $data
     */
    public function updateInfo($id, $data)
    {
        $this->db->where('id', $id);
        return $this->db->update($this->_table, $data);
    }

    /**
     * 查询中奖者名单
     */
    public function findWinningList($lotteryId)
    {
        $this->db->select('user_name, lottery_winner.prize_name');
        $this->db->from($this->_table);
        $this->db->where('lottery_id', $lotteryId);
        $this->db->where('lottery_winner.created_at > ', date('Y-m-d'));
        return $this->db->get();
    }

    /**
     * 总中奖人数
     */
    public function todayTotal($params, $today = false)
    {
        $this->db->select('*');
        $this->db->from($this->_table);
        if ( !empty($params['lottery_id'])) {
            $this->db->where_in('lottery_winner.lottery_id', $params['lottery_id']);
        }

        if ( !empty($params['reseller_name'])) {
            $this->db->where('lottery_winner.reseller_name', $params['reseller_name']);
        }
        if ($today) {
            $this->db->where('lottery_winner.created_at > ', date('Y-m-d')); //今天总中奖人数
        }
        return $this->db->count_all_results();
    }

    /**
     * 中奖总人数
     */
    public function winersTotal($params)
    {
        $this->db->select('*');
        $this->db->from($this->_table);
        $this->db->where_in('lottery_id', $params['lottery_id']);

        if ( !empty($params['status'])) {
            $this->db->where('lottery_winner.status', $params['status']);
        }

        if ( !empty($params['prize_type'])) {
            $this->db->where('lottery_winner.prize_type', $params['prize_type']);
        }

        if ( !empty($params['grade'])) {
            $this->db->where('lottery_winner.grade', $params['grade']);
        }

        if ( !empty($params['user_name'])) {
            $this->db->like('lottery_winner.user_name', $params['user_name'], 'after');
        }

        if ( !empty($params['reseller_name'])) {
            $this->db->where('lottery_winner.reseller_name', $params['reseller_name']);
        }

        if ( !empty($params['dateType'])) {
            $startDate = ($params['dateType'] == 'year') ? date('Y-m-d H:i:s', strtotime("-1 year")) : date('Y-m-d H:i:s', strtotime("-{$params['dateType']} day"));
            $this->db->where('created_at > ', $startDate);
            $this->db->where('created_at < ', date('Y-m-d H:i:s'));
        }

        /**
         * 单独搜索用户名时
         */
        if ( !empty($params['username_key'])) {
            $this->db->where('prize_type', 4); //现场颁奖
            $this->db->like('lottery_winner.user_name', $params['username_key'], 'after');
        }

        return $this->db->count_all_results();
    }

    /**
     * 查询中奖人数
     */
    public function winnersList($params, $start, $count)
    {
        $this->db->select('lottery_winner.id, lottery_winner.lottery_id, uid, user_name, lottery_winner.prize_name, prize_type, lottery_winner.amount, lottery_winner.status, lottery_winner.grade, lottery_winner.effective_date, lottery_winner.created_at, lottery_prize.image');
        $this->db->from($this->_table);
        $this->db->join($this->_table1, 'lottery_winner.prize_id=lottery_prize.prize_id', 'INNER');
        $this->db->where_in('lottery_winner.lottery_id', $params['lottery_id']);

        if ( !empty($params['status'])) {
            $this->db->where('lottery_winner.status', $params['status']);
        }

        if ( !empty($params['prize_type'])) {
            $this->db->where('lottery_winner.prize_type', $params['prize_type']);
        }

        if ( !empty($params['grade'])) {
            $this->db->where('lottery_winner.grade', $params['grade']);
        }

        if ( !empty($params['user_name'])) {
            $this->db->like('lottery_winner.user_name', $params['user_name'], 'after');
        }

        if ( !empty($params['reseller_name'])) {
            $this->db->where('lottery_winner.reseller_name', $params['reseller_name']);
        }

        if ( !empty($params['dateType'])) {
            $startDate = ($params['dateType'] == 'year') ? date('Y-m-d H:i:s', strtotime("-1 year")) : date('Y-m-d H:i:s', strtotime("-{$params['dateType']} day"));
            $this->db->where('lottery_winner.created_at > ', $startDate);
            $this->db->where('lottery_winner.created_at < ', date('Y-m-d H:i:s'));
        }

        /**
         * 单独搜索用户名时
         */
        if ( !empty($params['username_key'])) {
            $this->db->where('prize_type', 4); //现场颁奖
            $this->db->like('lottery_winner.user_name', $params['username_key'], 'after');
        }

        $this->db->order_by('lottery_winner.id', 'DESC');
        $this->db->limit($count, $start);
        return $this->db->get();
    }
}